package gmp

import (
	"fmt"
	"unicode/utf8"
)

type Stu struct {
}

func init1() {
	var b = "Go语言"
	for index, value := range b {
		//符＃U可用于打印符文数十六进制
		//的Unicode编码方式及字符形状
		fmt.Printf("%#U starts at byte position %d\n", value, index)
	}

	fmt.Println("--------------------------------------------->")
	for i, w := 0, 0; i < len(b); i += w {
		value, width := utf8.DecodeRuneInString(b[i:])
		fmt.Printf("%#U starts at byte position %d len: %d \n", value, i, width)
		w = width
	}

	dd := make(map[string]interface{})
	fmt.Println(dd)
}

type LineMap struct {
	data []interface{}
}

type Ele struct {
}

func (self *LineMap) add(ele interface{}) {
	pos := self.hash(ele)
	self.setValue(pos, ele)
}

func (self *LineMap) adds(eles []int) {
	for _, v := range eles {
		self.add(v)
	}
}

func (self *LineMap) hash(ele interface{}) int {
	return ele.(int) % 5
}

func (self *LineMap) show() {
	for i, v := range self.data {
		fmt.Println("[", i, "]=>", v)
	}
}

func (self *LineMap) extendMap(size int) {
	if self.data == nil {
		self.data = make([]interface{}, size)
	} else {
		expectSize := int(float64(size) * 0.75)
		if len(self.data) >= expectSize {
			newData := make([]interface{}, size*2)
			for i, v := range self.data {
				newData[i] = v
			}
			self.data = newData
		}
	}
}

func (self *LineMap) setValue(pos int, ele interface{}) {
	self.extendMap(pos + 1)
	v := self.data[pos]
	if v == nil {
		self.data[pos] = ele
	} else {
		self.setValue(pos+1, ele)
	}
}

func (self *LineMap) remove(ele interface{}) bool {
	pos := self.hash(ele)
	find := -1
	for i, v := range self.data[pos:] {
		if v == ele {
			fmt.Println("find it ", ele)
			self.data[i+pos] = nil
			find = i + pos
		}
	}
	return find > 0
}

func init() {
	//lmap := &LineMap{}
	//lmap.adds([]int{10, 11, 17, 13, 18})
	//lmap.show()
	//var flag = lmap.remove(13)
	//fmt.Println("删除是否成功:", flag)
	//lmap.show()

}
